package a66weding.com.jiehuntong.common;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import a66weding.com.jiehuntong.MainActivity;
import a66weding.com.jiehuntong.beans.VersionBean;
import a66weding.com.jiehuntong.receiver.NetStateChangeReceiver;
import a66weding.com.jiehuntong.services.UpdateAppService;


/**
 * 软件版本相关控制器
 * Created by jh on 2016/7/4.
 */
public class UpdateAppController extends BaseController {

    private static VersionBean mVersionBean;


    public UpdateAppController(Context mContext) {

    }

    //    获取软件信息
    public static void handleGetUpdateAppByNet(String url, int nowVersion,String apkMd5) {

        RequestParams requestParams = new RequestParams(url);
        requestParams.addBodyParameter("appkey", "jiehuntong");
        requestParams.addBodyParameter("app_version", nowVersion+"");
        requestParams.addBodyParameter("channel", "");
        requestParams.addBodyParameter("old_md5", apkMd5);


        x.http().post(requestParams, new Callback.CacheCallback<String>() {
            @Override
            public boolean onCache(String result) {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                MainActivity.setIsNewVersion(true);
                if(!TextUtils.isEmpty(result)){
                    Gson gson = new Gson();
                    VersionBean versionBean = gson.fromJson(result, VersionBean.class);

                    EventBus.getDefault().post(versionBean);

                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });

    }

    /**
     * 开启服务去获取软件更新信息
     *
     * @param context
     */
    public static void startUpdateService(Context context) {
        Intent service = new Intent();
        service.setClass(context, UpdateAppService.class);
        context.startService(service);
    }

    /**
     * 开启服务去断点下载更新
     *
     * @param context
     */
    public static void startUpdateServiceForDownContinue(Context context, String flag, String value) {
        Intent service = new Intent();
        service.setClass(context, UpdateAppService.class);
        service.putExtra(flag, value);
        context.startService(service);
    }

    /**
     * 开启服务去下载软件
     *
     * @param context
     */
    public static void startUpdateServiceForDown(Context context, String flag, boolean value,VersionBean verBean) {
       mVersionBean=verBean;

        Intent service = new Intent();
        service.setClass(context, UpdateAppService.class);
        service.putExtra(flag, value);
        Bundle bundle = new Bundle();
        bundle.putSerializable("version",mVersionBean);
        service.putExtras(bundle);
        context.startService(service);
    }

    /**
     * 停止服务
     */
    public static void stopUpdateService(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, UpdateAppService.class);
        context.stopService(intent);
    }
    /**
     * 注册接收网络改变广播
     */
    public static void registerNetStateReceiver(Context context, NetStateChangeReceiver mNetChangedReceiver) {
        if (mNetChangedReceiver != null) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            context.registerReceiver(mNetChangedReceiver, filter);
        }
    }

    /**
     * 解注册广播
     */
    public static void unRegistrNetStateReceiver(Context context, NetStateChangeReceiver mWifiReceiver) {
        if (mWifiReceiver != null) {
            context.unregisterReceiver(mWifiReceiver);
        }
    }

}
